"""检查URL配置是否完全匹配"""
from config import settings
from urllib.parse import unquote

project_url = settings.DINGTALK_REDIRECT_URI
print("=" * 70)
print("回调域名对比检查")
print("=" * 70)
print(f"\n项目配置的回调域名:")
print(f"  原始: {project_url}")
print(f"  长度: {len(project_url)}")
print(f"  字符: {repr(project_url)}")

# 钉钉后台配置的URL（从图片中看到）
dingtalk_url = "http://localhost:5173/dingtalk/callback"
print(f"\n钉钉后台配置的回调域名:")
print(f"  原始: {dingtalk_url}")
print(f"  长度: {len(dingtalk_url)}")
print(f"  字符: {repr(dingtalk_url)}")

# 对比
print(f"\n对比结果:")
if project_url == dingtalk_url:
    print("✅ 完全匹配")
else:
    print("❌ 不匹配")
    print("\n差异分析:")
    if len(project_url) != len(dingtalk_url):
        print(f"  长度不同: 项目={len(project_url)}, 钉钉={len(dingtalk_url)}")
    
    # 逐字符对比
    for i, (c1, c2) in enumerate(zip(project_url, dingtalk_url)):
        if c1 != c2:
            print(f"  位置 {i}: 项目='{c1}' (ascii {ord(c1)}), 钉钉='{c2}' (ascii {ord(c2)})")

# URL编码测试
print(f"\nURL编码测试:")
encoded = "http%3A%2F%2Flocalhost%3A5173%2Fdingtalk%2Fcallback"
decoded = unquote(encoded)
print(f"  编码后: {encoded}")
print(f"  解码后: {decoded}")
print(f"  解码后是否匹配项目: {'✅' if decoded == project_url else '❌'}")
print(f"  解码后是否匹配钉钉: {'✅' if decoded == dingtalk_url else '❌'}")

print("\n" + "=" * 70)
print("建议:")
print("1. 确保钉钉后台的URL和项目配置完全一致（包括大小写）")
print("2. 检查URL前后是否有空格")
print("3. 企业内部应用可能不支持localhost，考虑使用内网穿透")
print("=" * 70)

